/**
 * User: Obi
 * Date: 13.07.11
 * Time: 22:27
 */
package com.jediflasher.game_client.utils.logging {
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;

	public class LoggerView extends Sprite {

		private static const INFO_FORMAT:TextFormat = new TextFormat("Consolas, Courier", 13, 0xCECECE);

		private static const WARNING_FORMAT:TextFormat = new TextFormat("Consolas, Courier", 13, 0xFF0000);
		
		private static const ERROR_FORMAT:TextFormat = new TextFormat("Consolas, Courier", 13, 0xFF0000);

		public function LoggerView(stage:Stage) {
			this._stage = stage;
			this._stage.addEventListener(Event.RESIZE, this.handler_resize);
			this._stage.addEventListener(KeyboardEvent.KEY_DOWN, this.handler_keyUp);

			this.initUI();
			this.update();
		}

		/**
		 * @private
		 */
		private var _collapsed:Boolean = true;

		/**
		 * @private
		 */
		private var _logTextField:TextField;

		/**
		 * @private
		 */
		private var _inputTextField:TextField;

		/**
		 * @private
		 */
		private var _baseTf:TextField;

		/**
		 * @private
		 */
		private var _stage:Stage;

		/**
		 * @private
		 */
		private var _w:Number;

		/**
		 * @private
		 */
		private var _h:Number;

		/**
		 * @private
		 */
		private var _autoScroll:Boolean = true;

		public function addError(message:String):void {
			this.add(message, ERROR_FORMAT);
		}

		public function addWarning(message:String):void {
			this.add(message, WARNING_FORMAT);
		}

		public function addInfo(message:String):void {
			this.add(message, INFO_FORMAT);
		}

		/**
		 * @private
		 */
		private function add(message:String, textFormat:TextFormat):void {
			var last:int = this._logTextField.text.length;
			this._logTextField.appendText(message);
			this._logTextField.setTextFormat(textFormat, last, this._logTextField.text.length);
			this._logTextField.appendText("\n");

			if(this._autoScroll) this._logTextField.scrollV = this._logTextField.maxScrollV;
		}

		/**
		 * @private
		 */
		private function initUI():void {
			this._logTextField = new TextField();
			this._logTextField.addEventListener(Event.SCROLL, this.handler_logTfScroll);
			//this._logTextField.background =true;
			super.addChild(this._logTextField);

			this._inputTextField = new TextField();
			this._inputTextField.defaultTextFormat = INFO_FORMAT;
			this._inputTextField.multiline = false;
			this._inputTextField.type = TextFieldType.INPUT;
			//this._inputTextField.background = true;
			//this._inputTextField.backgroundColor = 0xC4C4C4;
			super.addChild(this._inputTextField);

			this._baseTf = new TextField();
			this._baseTf.selectable = false;
			this._baseTf.defaultTextFormat = INFO_FORMAT;
			this._baseTf.text = "> ";
			super.addChild(this._baseTf);
		}

		private function drawUI():void {
			// draw bg
			var g:Graphics = this.graphics;
			g.clear();
			g.lineStyle(1, 0x0);
			g.beginFill(0x0, 0.9);
			g.drawRect(0, 0, this._w, this._h);
			g.endFill();
		}

		/**
		 * @private
		 */
		private function update():void {
			this._w = this._stage.stageWidth;
			this._h = this._stage.stageHeight / 1.3;
			this.drawUI();

			var inputH:Number = 20;

			this._logTextField.y = 5;
			this._logTextField.x = 5;
			this._logTextField.width = this._w - 10;
			this._logTextField.height = this._h - inputH - 15;
			this._logTextField.wordWrap = true;

			this._baseTf.x = this._logTextField.x;
			this._baseTf.y = this._logTextField.y + this._logTextField.height + 5;
			this._baseTf.width = 10;
			this._baseTf.height = inputH;

			this._inputTextField.x = this._baseTf.x + this._baseTf.width + 5;
			this._inputTextField.y = this._baseTf.y;
			this._inputTextField.width = this._logTextField.width - this._baseTf.width - 5;
			this._inputTextField.height = inputH;

			if(this._collapsed) this.y = -this.height;
			else this.y = 0;
		}

		/**
		 * @private
		 */
		private function expand():void {
			if(!this._collapsed) return;

			this._collapsed = false;
			this.y = 0;
			this._stage.focus = this._inputTextField;
		}

		/**
		 * @private
		 */
		private function collapse():void {
			if(this._collapsed) return;

			this._collapsed = true;
			this.y = -this.height;
		}

		/**
		 * @private
		 */
		private function handler_keyUp(event:KeyboardEvent):void {
			switch(event.keyCode) {
				case 192:
					// чтобы при нажатии на кнопку не записалось в консоль
					this._stage.focus = null;
					this._collapsed ? this.expand() : this.collapse();
				break;
				case Keyboard.ENTER:
					if(this._stage.focus === this._inputTextField) {
						if(this._inputTextField.text.indexOf("error:") == 0) {
							this.addError("console> "+this._inputTextField.text);
						}else if(this._inputTextField.text.indexOf("warning:") == 0) {
							this.addWarning("console> "+this._inputTextField.text);
						}else {
							this.addInfo("console> "+this._inputTextField.text);
						}

						super.dispatchEvent(new LoggerEvent(LoggerEvent.INPUT, this._inputTextField.text));

						this._inputTextField.text = "";
					}
				break;
			}
		}

		/**
		 * @private
		 */
		private function handler_resize(event:Event):void {
			this.update();
		}

		/**
		 * @private
		 */
		private function handler_logTfScroll(event:Event):void {
			this._autoScroll = this._logTextField.scrollV != this._logTextField.maxScrollV;
		}
	}
}
